﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
using System.Web.UI.Design;
using System.Drawing.Design;
using System.Drawing;
using Water.Web.Controls.Design;

namespace Water.Web.Controls
{
    [ToolboxData("<{0}:GridFormat runat=server></{0}:GridFormat>")]
    [Designer(typeof(LayoutControlDesigner)), ControlBuilder(typeof(GridRowFormatControlBuilder)), ParseChildren(false), PersistChildren(true)]
    public class GridRowFormat:WebControl
    {
        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "default_g_rows");
        }

        protected override void RenderChildren(HtmlTextWriter writer)
        {
            //base.RenderChildren(writer);
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "default_g_rows_table");
            writer.RenderBeginTag(HtmlTextWriterTag.Table); //table begin

            writer.AddAttribute(HtmlTextWriterAttribute.Class, "default_g_row_edit");
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);//tr begin

            int _cellIndex = 0;
            foreach (Control _c in this.Controls)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "default_gc");
                writer.AddAttribute(HtmlTextWriterAttribute.Axis, _cellIndex++ + "");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);//td begin

                _c.RenderControl(writer);//render control

                writer.RenderEndTag();//td end

            }

            writer.RenderEndTag();//tr end
            writer.RenderEndTag();//table end
        }
    }

    internal class GridRowFormatControlBuilder : ControlBuilder
    {
        public override bool AllowWhitespaceLiterals()
        {
            return false;
        }
    }
}
